# -*- coding: utf-8 -*-
'''eof
name:最近6个月平均应还款占比
code:AvgRepaymentPercent_M6_2D
tableName:
columnName:
groups:个人征信
dependencies:IC_DSJ_2D
type:常用指标
dataSourceType:
eof'''

import sys
reload(sys)
sys.setdefaultencoding('utf-8')
null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']

def getTable(data, tableName, occurTimes):
    '''
        获取表信息
        :param data:表上层的字典对象
        :param tableName:表的名字
        :param occurTimes:表出现的次数，如果是0代表此表可以不出现，如果是1代表此表必须出现
        :return:该表全部数据
        '''
    table = data.get(tableName)
    if occurTimes == 0:
        if table is None or table == "":
            return [{}]
        elif isinstance(table, list) is False or len(table) == 0:
            return None
        else:
            return data.get(tableName)
    if occurTimes == 1:
        if table == "":
            return [{}]
        elif table is None or isinstance(table, list) is False or len(table) == 0:
            return None
        elif len(table) == 0:
            return [{}]
        else:
            return data.get(tableName)

def isfloat(x):
    '''
    判断x为float类型且大于等于0
    :param x:
    :return:
    '''
    try:
        if isinstance(float(x), float) and float(x)>=0:
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False

def value(x,y):
     if x.has_key(y) and isfloat(x[y])==True and x[y] not in null_type_list:
         return float(x[y])
     else:
         return 0



def IC4_AvgRepaymentPercent_M6():
    factor1 = []
    factor2 = []

    data = IC_DSJ_2D['data']

    try:
        PCO=getTable(data,"PCO",1)
        if PCO is None:
            return u"缺失值"
        elif PCO == [{}]:
            return u"缺失值"
        else:
            PC02 = getTable(PCO[0], 'PC02', 0)
            if PC02 is None:
                return u'缺失值'
            elif PC02 == [{}]:
                return 0
            else:
                try:
                    # 1非循环贷账户汇总信息
                    PC02E = getTable(PC02[0], 'PC02E', 0)
                    ##最近6个月平均应还款
                    PC02EJ03 = PC02E[0].get('PC02EJ03')
                    # PC02EJ03=getTable(PC02E, 'PC02EJ03', 1)
                    ##授信总额
                    PC02EJ01=PC02E[0].get( 'PC02EJ01')

                    # 2循环贷额度分账户汇总
                    PC02F = getTable(PC02[0], 'PC02F', 0)
                    ##最近6个月平均应还款
                    PC02FJ03 = PC02F[0].get( 'PC02FJ03')
                    ##授信总额
                    PC02FJ01= PC02F[0].get('PC02FJ01')

                    # 3循环贷账户汇总
                    PC02G = getTable(PC02[0], 'PC02G', 0)
                    ##最近6个月平均应还款
                    PC02GJ03 = PC02G[0].get('PC02GJ03')
                    ##授信总额
                    PC02GJ01= PC02G[0].get('PC02GJ01')


                    # 4贷记卡账户汇总
                    PC02H = getTable(PC02[0], 'PC02H', 0)
                    ##最近6个月平均应还款
                    PC02HJ05 =PC02H[0].get('PC02HJ05')
                    ##授信总额
                    PC02HJ01= PC02H[0].get('PC02HJ01')


                    # 5准贷记卡账户汇总
                    PC02I = getTable(PC02[0], 'PC02I', 0)
                    ##最近6个月平均应还款
                    PC02IJ05 = PC02I[0].get('PC02IJ05')
                    ##授信总额
                    PC02IJ01= PC02I[0].get('PC02IJ01')

                    if   isfloat(PC02EJ03)>0 or isfloat(PC02FJ03)>0 or \
                         isfloat(PC02GJ03) > 0 or isfloat(PC02HJ05) > 0 or \
                         isfloat(PC02IJ05) > 0:

                        factor1.append(PC02EJ03 )
                        factor1.append(PC02FJ03 )
                        factor1.append( PC02GJ03 )
                        factor1.append( PC02HJ05 )
                        factor1.append( PC02IJ05)

                    else:
                        factor1.append(None)

                    if   isfloat(PC02EJ01)>0 or isfloat(PC02FJ01)>0 or \
                         isfloat(PC02GJ01) > 0 or isfloat(PC02HJ01) > 0 or \
                         isfloat(PC02IJ01) > 0:

                        factor2.append(PC02EJ01 )
                        factor2.append( PC02FJ01)
                        factor2.append(PC02GJ01 )
                        factor2.append( PC02HJ01 )
                        factor2.append( PC02IJ01)

                    else:

                        factor2.append(None)
                except:
                    factor1.append(None)
                    factor2.append(None)
                    # continue

        factor1NotNone = filter(lambda x: x is not None, factor1)
        factor2NotNone = filter(lambda x: x is not None, factor2)
        a=[]
        for i in range(len(factor1NotNone)):

            b = float(factor1NotNone[i])
            a.append(b)
        sumfactor1 =sum(a)
        c=[]
        for i in range(len(factor2NotNone)):
            # print i
            # b=0
            d = float(factor2NotNone[i])
            c.append(d)
        sumfactor2 =sum(c)

        if len(factor1NotNone) == 0 or len(factor2NotNone) == 0 :
            return u'缺失值'
        else:
            return float(sumfactor1/sumfactor2)
    except:
        return u'缺失值'

if __name__ == '__main__':
    import json
    file_obj = open(r"..\json\IC_DSJ_2D.json", 'r')
    content = file_obj.read()
    IC_DSJ_2D = json.loads(content, strict=False)['IC_DSJ_2D']

    print IC4_AvgRepaymentPercent_M6()


result=IC4_AvgRepaymentPercent_M6()